package com.lx.mobileplayer.mvp.view.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * 类说明：Adapter基类
 * Created by liuxiong on 2019/4/25.
 */
abstract class BaseAdater<T>(private var layoutId:Int,var data:ArrayList<T>) :RecyclerView.Adapter<BaseAdater.BaseViewHodler>(){

    var mItemClickListener:OnItemClickListener?=null

    /**
     * 方法说明:  设置新的数据
     * created by liuxiong on 2019/4/25 14:30
     */
    open fun setNewData(newData:ArrayList<T>){
        this.data.clear()
        addData(newData)
    }

    /**
     * 方法说明:  添加数据
     * created by liuxiong on 2019/4/25 14:30
     */
    open fun addData(addData:ArrayList<T>){
        data.addAll(addData)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHodler {
        var view=LayoutInflater.from(parent.context).inflate(layoutId,parent,false)

        return BaseViewHodler(view)
    }

    override fun getItemCount(): Int {

        return data.size
    }

    override fun getItemViewType(position: Int): Int {


        return super.getItemViewType(position)
    }

    override fun onBindViewHolder(holder: BaseViewHodler, position: Int) {
        val itemBean = data.get(position)

        holder.itemView.setOnClickListener {
            mItemClickListener?.let {
                mItemClickListener?.onItemClick(holder.itemView,position)
            }
        }

        onBindDataToView(holder,itemBean)
    }

    fun setOnItemClickListener(listener:OnItemClickListener){
        mItemClickListener=listener
    }
    
    
    /**
    * 方法说明:  绑定数据到对应的控件
    * created by liuxiong on 2019/4/25 16:00
    */
    abstract fun onBindDataToView(holder: BaseViewHodler, bean:T)


    interface OnItemClickListener{
        fun onItemClick(itemView:View,position:Int)
    }
    

    /**
    *  类说明：ViewHodler基类
    *  created by liuxiong on 2019/4/25
    **/
    class BaseViewHodler(itemView: View): RecyclerView.ViewHolder(itemView) {

        fun <E : View?> getView(viewId:Int):E{
            adapterPosition

            return itemView.findViewById<E>(viewId)
        }
    }


}